<?php
namespace App\Services;
use App\Models\logRoleDeath;
use App\DTOS\Admin\LogRoleDeathSearchDTO;
use App\Utils\MyLog;

// 角色死亡日志
class LogRoleDeathService
{
    /**
    * @var  \Illuminate\Database\Eloquent\Builder
    */
    private logRoleDeath $model;

    public function __construct()
    {
        $this->model = new logRoleDeath;
        global $gameArea;
        $this->model->setGameArea($gameArea);
    }

    public function paginate(LogRoleDeathSearchDTO $DTO)
    {
        $perPage = $DTO->getLimit();
        $page = $DTO->getPage();
        $model = $this->model;
        /**条件判断**/
        $mapId = $DTO->getMapId();
        $memId = $DTO->getMemId();
        $roleId = $DTO->getRoleId();
        $nick = $DTO->getNick();
        $killId = $DTO->getKillId();
        $killType = $DTO->getKillType();
        $gameArea = $DTO->getGameArea();
        $createDateBegin = $DTO->getCreateDateBegin();
        $createDateEnd = $DTO->getCreateDateEnd();
        $model = $model->setGameArea($gameArea);

        MyLog::info('开始时间'.json_encode(strtotime($createDateBegin)));
        if ($createDateBegin){
            $model =  $model->where("logTime",">=",strtotime($createDateBegin));
        }
        if ($createDateEnd){
            $model =  $model->where("logTime","<=",strtotime($createDateEnd));
        }
        if($mapId){
            $model = $model->where('map_id',$mapId);
        }
        if ($memId){
            $model = $model->where('memId',$memId);
        }
        if ($roleId){
            $model = $model->where('roleId',$roleId);
        }
        if ($nick){
            $model = $model->where('nick','like',"%$nick%");
        }
        if ($killId){
            $model = $model->where('kill_id',$killId);
        }
        if ($killType!==null){
            $model = $model->where('kill_type',$killType);
        }

        return $model->orderByDesc('logTime')->paginate($perPage,'*','page',$page);
    }


    public function deleteById($id)
    {
        $this->model->where('id',$id)->delete();
    }

    public function deleteByIds($ids)
    {
        if (empty($ids)) return;
        $this->model->whereIn('id',$ids)->delete();
    }
    public function getById($id)
    {
        return $this->model->where('id',$id)->first();
    }
}
